/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	uart.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-05-22
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "uart.h"


/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/



 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/
static void bsp_uart_irq_handler(bsp_uart_t * p_uart);

/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
static bsp_uart_t m_bsp_uart1;
static bsp_uart_t m_bsp_uart2;
static bsp_uart_t m_bsp_uart3;
static bsp_uart_t m_bsp_uart4;
static bsp_uart_t m_bsp_uart6;
static bsp_uart_t m_bsp_uart7;
static bsp_uart_t m_bsp_uart8;
/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/

bsp_uart_t* bsp_uart_init(USART_TypeDef* uart,uint32_t baudrate)
{
	bsp_uart_t* p_bsp_uart;
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.Mode=GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull=GPIO_PULLUP;
	GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_HIGH;
	
	switch((uint32_t)uart){
		case (uint32_t)USART1:{
			__HAL_RCC_GPIOA_CLK_ENABLE();
			__HAL_RCC_USART1_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_9;
			GPIO_InitStruct.Alternate=GPIO_AF7_USART1;
			HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
			GPIO_InitStruct.Pin=GPIO_PIN_10;
			HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
			
			p_bsp_uart=&m_bsp_uart1;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART1_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_USART1_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART1_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_USART1_RX;
			
			HAL_NVIC_EnableIRQ(USART1_IRQn);
			HAL_NVIC_SetPriority(USART1_IRQn,2,2);
			break;
		}
		case (uint32_t)USART2:{
			__HAL_RCC_GPIOD_CLK_ENABLE();
			__HAL_RCC_USART2_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_5;
			GPIO_InitStruct.Alternate=GPIO_AF7_USART2;
			HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);
			GPIO_InitStruct.Pin=GPIO_PIN_6;
			HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);
			
			p_bsp_uart=&m_bsp_uart2;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART2_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_USART2_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART2_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_USART2_RX;
			
			HAL_NVIC_EnableIRQ(USART2_IRQn);
			HAL_NVIC_SetPriority(USART2_IRQn,2,2);
			break;
		}
		case (uint32_t)USART3:{
			__HAL_RCC_GPIOD_CLK_ENABLE();
			__HAL_RCC_GPIOB_CLK_ENABLE();
			__HAL_RCC_USART3_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_8;
			GPIO_InitStruct.Alternate=GPIO_AF7_USART3;
			HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);
			GPIO_InitStruct.Pin=GPIO_PIN_9;
			HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);
			
			__HAL_RCC_GPIOE_CLK_ENABLE();
			GPIO_InitTypeDef  GPIO_InitStruct;
			GPIO_InitStruct.Mode      = GPIO_MODE_OUTPUT_PP;
			GPIO_InitStruct.Pull      = GPIO_PULLUP;
			GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
			GPIO_InitStruct.Pin       = USART3_422DE_GPIO_PIN;
			HAL_GPIO_Init(USART3_422DE_GPIO_BASE, &GPIO_InitStruct);
			GPIO_InitStruct.Pin       = USART3_422RE_GPIO_PIN;
			HAL_GPIO_Init(USART3_422RE_GPIO_BASE, &GPIO_InitStruct);
			
//			HAL_GPIO_WritePin(USART3_422DE_GPIO_BASE,USART3_422DE_GPIO_PIN,GPIO_PIN_SET);
//			HAL_GPIO_WritePin(USART3_422RE_GPIO_BASE,USART3_422RE_GPIO_PIN,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(USART3_422DE_GPIO_BASE,USART3_422DE_GPIO_PIN,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(USART3_422RE_GPIO_BASE,USART3_422RE_GPIO_PIN,GPIO_PIN_RESET);
			
			p_bsp_uart=&m_bsp_uart3;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART3_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_USART3_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART3_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_USART3_RX;
			
			HAL_NVIC_EnableIRQ(USART3_IRQn);
			HAL_NVIC_SetPriority(USART3_IRQn,2,2);
			break;
		}
		case (uint32_t)UART7:{
			__HAL_RCC_GPIOE_CLK_ENABLE();
			__HAL_RCC_UART7_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_7;
			GPIO_InitStruct.Alternate=GPIO_AF7_UART7;
			HAL_GPIO_Init(GPIOE,&GPIO_InitStruct);
			GPIO_InitStruct.Pin=GPIO_PIN_8;
			HAL_GPIO_Init(GPIOE,&GPIO_InitStruct);
			
			GPIO_InitTypeDef  GPIO_InitStruct;
			GPIO_InitStruct.Mode      = GPIO_MODE_OUTPUT_PP;
			GPIO_InitStruct.Pull      = GPIO_PULLUP;
			GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
			GPIO_InitStruct.Pin       = UART7_485DE_GPIO_PIN;
			HAL_GPIO_Init(UART7_485DE_GPIO_BASE, &GPIO_InitStruct);
			
			HAL_GPIO_WritePin(UART7_485DE_GPIO_BASE,UART7_485DE_GPIO_PIN,GPIO_PIN_RESET);
			
			p_bsp_uart=&m_bsp_uart7;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART7_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_UART7_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART7_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_UART7_RX;
			
			HAL_NVIC_EnableIRQ(UART7_IRQn);
			HAL_NVIC_SetPriority(UART7_IRQn,2,2);
			break;
		}
		case (uint32_t)USART6:{
			__HAL_RCC_GPIOC_CLK_ENABLE();
			__HAL_RCC_USART6_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_6|GPIO_PIN_7;
			GPIO_InitStruct.Alternate=GPIO_AF7_USART6;
			HAL_GPIO_Init(GPIOC,&GPIO_InitStruct);
			
			p_bsp_uart=&m_bsp_uart6;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART6_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_USART6_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART6_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_USART6_RX;
			
			HAL_NVIC_EnableIRQ(USART6_IRQn);
			HAL_NVIC_SetPriority(USART6_IRQn,2,2);
			break;
		}
		case (uint32_t)UART8:{
			__HAL_RCC_GPIOE_CLK_ENABLE();
			__HAL_RCC_UART8_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_0|GPIO_PIN_1;
			GPIO_InitStruct.Alternate=GPIO_AF8_UART8;
			HAL_GPIO_Init(GPIOE,&GPIO_InitStruct);
			
			p_bsp_uart=&m_bsp_uart8;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART8_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_UART8_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART8_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_UART8_RX;
			
			HAL_NVIC_EnableIRQ(UART8_IRQn);
			HAL_NVIC_SetPriority(UART8_IRQn,2,2);
			break;
		}
		case (uint32_t)UART4:{
			__HAL_RCC_GPIOD_CLK_ENABLE();
			__HAL_RCC_UART4_CLK_ENABLE();

			GPIO_InitStruct.Pin=GPIO_PIN_0|GPIO_PIN_1;
			GPIO_InitStruct.Alternate=GPIO_AF8_UART4;
			HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);
			
			p_bsp_uart=&m_bsp_uart4;
			
			p_bsp_uart->hal_tx_dma_handler.Instance=UART4_TX_DMA_STREAM;
			p_bsp_uart->hal_tx_dma_handler.Init.Request=DMA_REQUEST_UART4_TX;
			
			p_bsp_uart->hal_rx_dma_handler.Instance=UART4_RX_DMA_STREAM;
			p_bsp_uart->hal_rx_dma_handler.Init.Request=DMA_REQUEST_UART4_RX;
			
			HAL_NVIC_EnableIRQ(UART4_IRQn);
			HAL_NVIC_SetPriority(UART4_IRQn,2,2);
			break;
		}
		default:return NULL;
	}
	
	bsp_uart_txen(p_bsp_uart);
	bsp_uart_rxen(p_bsp_uart);
	
	p_bsp_uart->hal_uart_handler.Instance=uart;
	p_bsp_uart->hal_uart_handler.Init.BaudRate=baudrate;
	p_bsp_uart->hal_uart_handler.Init.WordLength=UART_WORDLENGTH_8B;
	p_bsp_uart->hal_uart_handler.Init.StopBits=UART_STOPBITS_1;
	p_bsp_uart->hal_uart_handler.Init.Parity=UART_PARITY_NONE;
	if(uart == USART3)
	{
#ifdef USE_RFID_MODBUS
	p_bsp_uart->hal_uart_handler.Init.WordLength=UART_WORDLENGTH_9B;
	p_bsp_uart->hal_uart_handler.Init.Parity=UART_PARITY_EVEN;
#else
#endif
	}
	
	p_bsp_uart->hal_uart_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
	p_bsp_uart->hal_uart_handler.Init.Mode=UART_MODE_TX_RX;
	HAL_UART_Init(&p_bsp_uart->hal_uart_handler);
	
	__HAL_UART_DISABLE_IT(&p_bsp_uart->hal_uart_handler,UART_IT_IDLE);
	__HAL_RCC_DMA1_CLK_ENABLE();
	__HAL_RCC_DMA2_CLK_ENABLE();
	return p_bsp_uart;
}

void bsp_uart_rx_dma_init(bsp_uart_t* p_bsp_uart,uart_callback pfn_callback,uint8_t* rxbuff,uint16_t maxsize)
{
	if(p_bsp_uart==NULL)
		return;
	p_bsp_uart->uart_idle_callback=pfn_callback;
	p_bsp_uart->rxbuff=rxbuff;
	p_bsp_uart->rxsize=maxsize;
	
//	__HAL_LINKDMA(&p_bsp_uart->hal_uart_handler,hdmarx,p_bsp_uart->hal_rx_dma_handler);

	p_bsp_uart->hal_rx_dma_handler.Init.Direction=DMA_PERIPH_TO_MEMORY;
	p_bsp_uart->hal_rx_dma_handler.Init.PeriphInc=DMA_PINC_DISABLE;
	p_bsp_uart->hal_rx_dma_handler.Init.MemInc=DMA_MINC_ENABLE;
	p_bsp_uart->hal_rx_dma_handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_BYTE;
	p_bsp_uart->hal_rx_dma_handler.Init.MemDataAlignment=DMA_MDATAALIGN_BYTE;
	p_bsp_uart->hal_rx_dma_handler.Init.Mode=DMA_NORMAL;
	p_bsp_uart->hal_rx_dma_handler.Init.Priority=DMA_PRIORITY_MEDIUM;
	p_bsp_uart->hal_rx_dma_handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;
	p_bsp_uart->hal_rx_dma_handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
	p_bsp_uart->hal_rx_dma_handler.Init.MemBurst=DMA_MBURST_SINGLE;
	p_bsp_uart->hal_rx_dma_handler.Init.PeriphBurst=DMA_PBURST_SINGLE;
	
//	HAL_DMA_DeInit(&p_bsp_uart->hal_rx_dma_handler);   
	HAL_DMA_Init(&p_bsp_uart->hal_rx_dma_handler);
	SET_BIT(p_bsp_uart->hal_uart_handler.Instance->CR3, USART_CR3_DMAR);
	HAL_DMA_Start(&p_bsp_uart->hal_rx_dma_handler,(uint32_t)&p_bsp_uart->hal_uart_handler.Instance->RDR,(uint32_t)p_bsp_uart->rxbuff,p_bsp_uart->rxsize);

	__HAL_UART_ENABLE_IT(&p_bsp_uart->hal_uart_handler,UART_IT_IDLE);
}
void bsp_uart_tx_dma_init(bsp_uart_t* p_bsp_uart,uart_callback pfn_callback)
{
	if(p_bsp_uart==NULL)
		return;
	p_bsp_uart->uart_tc_callback=pfn_callback;

//	__HAL_LINKDMA(&p_bsp_uart->hal_uart_handler,hdmatx,p_bsp_uart->hal_tx_dma_handler);
	
	p_bsp_uart->hal_tx_dma_handler.Init.Direction=DMA_MEMORY_TO_PERIPH;
	p_bsp_uart->hal_tx_dma_handler.Init.PeriphInc=DMA_PINC_DISABLE;
	p_bsp_uart->hal_tx_dma_handler.Init.MemInc=DMA_MINC_ENABLE;
	p_bsp_uart->hal_tx_dma_handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_BYTE;
	p_bsp_uart->hal_tx_dma_handler.Init.MemDataAlignment=DMA_MDATAALIGN_BYTE;
	p_bsp_uart->hal_tx_dma_handler.Init.Mode=DMA_NORMAL;
	p_bsp_uart->hal_tx_dma_handler.Init.Priority=DMA_PRIORITY_MEDIUM;
	p_bsp_uart->hal_tx_dma_handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;
	p_bsp_uart->hal_tx_dma_handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
	p_bsp_uart->hal_tx_dma_handler.Init.MemBurst=DMA_MBURST_SINGLE;
	p_bsp_uart->hal_tx_dma_handler.Init.PeriphBurst=DMA_PBURST_SINGLE;
	
//	HAL_DMA_DeInit(&p_bsp_uart->hal_tx_dma_handler);   
	HAL_DMA_Init(&p_bsp_uart->hal_tx_dma_handler);
	SET_BIT(p_bsp_uart->hal_uart_handler.Instance->CR3, USART_CR3_DMAT);

	((DMA_Stream_TypeDef *)(&p_bsp_uart->hal_tx_dma_handler)->Instance)->NDTR = 0;
	__HAL_UART_ENABLE_IT(&p_bsp_uart->hal_uart_handler,UART_IT_TC);
}

void bsp_uart_dma_transfer(bsp_uart_t* p_bsp_uart,uint8_t* txbuff,uint16_t txsize)
{
	if(NULL==p_bsp_uart || txbuff == NULL || txsize == 0)
		return;
	
	bsp_uart_txen(p_bsp_uart);
	
	p_bsp_uart->txbuff=txbuff;
	p_bsp_uart->txsize=txsize;
	HAL_DMA_Abort(&p_bsp_uart->hal_tx_dma_handler);
	HAL_DMA_Start(&p_bsp_uart->hal_tx_dma_handler,(uint32_t)p_bsp_uart->txbuff,(uint32_t)&p_bsp_uart->hal_uart_handler.Instance->TDR,p_bsp_uart->txsize);

	__HAL_UART_CLEAR_FLAG(&p_bsp_uart->hal_uart_handler, UART_CLEAR_TCF);
	
//	HAL_UART_Transmit_DMA(&p_bsp_uart->hal_uart_handler,p_bsp_uart->txbuff,p_bsp_uart->txsize);
}
void bsp_uart_transfer(bsp_uart_t* p_bsp_uart,uint8_t* txbuff,uint16_t txsize)
{
	if(NULL==p_bsp_uart)return;
	bsp_uart_txen(p_bsp_uart);
	HAL_UART_Transmit(&p_bsp_uart->hal_uart_handler,txbuff,txsize,500);
	bsp_uart_rxen(p_bsp_uart);
}

void bsp_uart_reveive(bsp_uart_t* p_uart,uint32_t size,uint32_t timeout)
{
	if(p_uart == NULL || p_uart->rxbuff == NULL)
		return;
	HAL_UART_Receive(&p_uart->hal_uart_handler,p_uart->rxbuff,size,timeout);
}
void bsp_uart_reveive_byte(bsp_uart_t* p_uart,uint8_t* byte,uint32_t timeout)
{
	if(p_uart == NULL || byte == NULL)
		return;
	HAL_UART_Receive(&p_uart->hal_uart_handler,byte,1,timeout);
}
void USART1_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart1);
#ifdef RTOS
	rt_interrupt_leave();
#endif
} 
void USART2_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart2);
#ifdef RTOS
	rt_interrupt_leave();
#endif
}
void USART6_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart6);
#ifdef RTOS
	rt_interrupt_leave();
#endif
}
void UART4_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart4);
#ifdef RTOS
	rt_interrupt_leave();
#endif
}
void UART7_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart7);
#ifdef RTOS
	rt_interrupt_leave();
#endif
}
void UART8_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart8);
#ifdef RTOS
	rt_interrupt_leave();
#endif
}
void USART3_IRQHandler(void)
{
#ifdef RTOS
	rt_interrupt_enter();
#endif
	bsp_uart_irq_handler(&m_bsp_uart3);
#ifdef RTOS
	rt_interrupt_leave();
#endif
}
void DMA1_Stream0_IRQHandler(void)
{
	
	
}
void bsp_uart_txen(bsp_uart_t* p_uart)
{
	if(p_uart == NULL)
		return;
	switch((uint32_t)p_uart->hal_uart_handler.Instance)
	{
		case (uint32_t)UART7:
		{
			HAL_GPIO_WritePin(UART7_485DE_GPIO_BASE,UART7_485DE_GPIO_PIN,GPIO_PIN_SET);
			break;
		}
		case (uint32_t)USART3:
		{
			HAL_GPIO_WritePin(USART3_422DE_GPIO_BASE,USART3_422DE_GPIO_PIN,GPIO_PIN_SET);
			HAL_GPIO_WritePin(USART3_422RE_GPIO_BASE,USART3_422RE_GPIO_PIN,GPIO_PIN_SET);
			break;
		}
		default:break;
	}
}
void bsp_uart_rxen(bsp_uart_t* p_uart)
{
	if(p_uart == NULL)
		return;
	switch((uint32_t)p_uart->hal_uart_handler.Instance)
	{
		case (uint32_t)UART7:
		{
			HAL_GPIO_WritePin(UART7_485DE_GPIO_BASE,UART7_485DE_GPIO_PIN,GPIO_PIN_RESET);
			break;
		}
		case (uint32_t)USART3:
		{
			HAL_GPIO_WritePin(USART3_422DE_GPIO_BASE,USART3_422DE_GPIO_PIN,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(USART3_422RE_GPIO_BASE,USART3_422RE_GPIO_PIN,GPIO_PIN_RESET);
			break;
		}
		default:break;
	}
}
static void bsp_uart_irq_handler(bsp_uart_t * p_uart)
{
	uint16_t size;
	/*DMA 接受在如H743 存在chche的芯片上 接收完成一定要清除rxbuff的cache*/
	if(__HAL_UART_GET_IT(&(p_uart->hal_uart_handler),UART_IT_IDLE))  
	{
		__HAL_UART_CLEAR_IT(&(p_uart->hal_uart_handler),UART_CLEAR_IDLEF);
		HAL_DMA_Abort(&(p_uart->hal_rx_dma_handler));
		size=p_uart->rxsize-__HAL_DMA_GET_COUNTER(&(p_uart->hal_rx_dma_handler));
		if(p_uart->uart_idle_callback!=NULL && size!=0)
		{
			SCB_InvalidateDCache_by_Addr((uint32_t*)p_uart->rxbuff,p_uart->rxsize);
			p_uart->uart_idle_callback(p_uart->arg,p_uart->rxbuff,size);
		}
		HAL_DMA_Start(&(p_uart->hal_rx_dma_handler),(uint32_t)&(p_uart->hal_uart_handler.Instance->RDR),(uint32_t)p_uart->rxbuff,p_uart->rxsize);
	}
	if(__HAL_UART_GET_IT(&(p_uart->hal_uart_handler),UART_IT_TC))  
	{
//		__HAL_UART_CLEAR_IT(&(p_uart->hal_uart_handler),UART_CLEAR_TCF);
//		if(p_uart->uart_tc_callback != NULL && __HAL_DMA_GET_COUNTER(&(p_uart->hal_tx_dma_handler)) == 0)
//		{
//			HAL_DMA_Abort(&(p_uart->hal_tx_dma_handler));
//			size=p_uart->txsize;
//			p_uart->uart_tc_callback(p_uart->arg,p_uart->txbuff,size);
//			bsp_uart_rxen(p_uart);
//		}
		__HAL_UART_CLEAR_IT(&(p_uart->hal_uart_handler),UART_CLEAR_TCF);
		HAL_DMA_Abort(&(p_uart->hal_tx_dma_handler));
		size=p_uart->txsize - __HAL_DMA_GET_COUNTER(&(p_uart->hal_tx_dma_handler));
		if(p_uart->uart_tc_callback != NULL )
		{
			p_uart->uart_tc_callback(p_uart->arg,p_uart->txbuff,size);
		}
		bsp_uart_rxen(p_uart);
	}
	return;
}
void bsp_uart_wait_tx_dma_cplt(bsp_uart_t* p_uart)
{
	while(p_uart != NULL
		&& 0 != __HAL_DMA_GET_COUNTER(&(p_uart->hal_tx_dma_handler)) 
		)
	{
	}
}